﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.UI;
using Emgu.Util;
using Emgu.CV.Structure;
using GaborRec;

namespace Plate
{
    public partial class RecogForm : Form
    {
        Bitmap ori_Bitmap;
        string Filename;
        public RecogForm(string filename)
        {
            this.Filename = filename;
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "jpg files(*.jpg)|*.jpg|all files(*.*)|*.*";
            dlg.Multiselect = true;
            if (dlg.ShowDialog() != DialogResult.OK)
                return;
            if (dlg.FileNames.Length == 1)
            {
                ori_Bitmap = new Bitmap(dlg.FileName);
                this.Filename = dlg.FileName;
                this.Text = Filename;
                pictureBox1.Image = ori_Bitmap;
                pictureBox1.Refresh();
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            SignDetection.SignDetector detector = SignDetection.SignDetector.CreateSignDetector();
            SignDetection.PlatePositionResult result = detector.PlatePositioning(ori_Bitmap);
            Bitmap LV_Bit = ori_Bitmap.Clone(result.Position, System.Drawing.Imaging.PixelFormat.DontCare);
            this.pictureBox2.Image = LV_Bit;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            signs=this.Segment();
//            this.ChooseCharacterImg();
        }

        List<Bitmap> Segment()
        {
            SignDetection.SignDetector detector = SignDetection.SignDetector.CreateSignDetector();
            //List<Bitmap> signs = detector.SegmentSigns(ori_Bitmap);
            List<Bitmap> signs = detector.DetectSign(ori_Bitmap);
           
                if (signs.Count >= 1)
                    this.character1.Image = signs[0];
                if (signs.Count >= 2)
                    this.character2.Image = signs[1];
                if (signs.Count >= 3)
                    this.character3.Image = signs[2];
                if (signs.Count >= 4)
                    this.character4.Image = signs[3];
                if (signs.Count >= 5)
                    this.character5.Image = signs[4];
                if (signs.Count >= 6)
                    this.character6.Image = signs[5];
                if (signs.Count >= 7)
                    this.character7.Image = signs[6];

                return signs;
               
        }

        private void ChooseCharacterImg()
        {
            if (signs.Count != 0)
            {
                this.character1.MouseClick += new MouseEventHandler(character1_MouseClick);
                this.character2.MouseClick += new MouseEventHandler(character2_MouseClick);
                this.character3.MouseClick += new MouseEventHandler(character3_MouseClick);
                this.character4.MouseClick += new MouseEventHandler(character4_MouseClick);
                this.character5.MouseClick += new MouseEventHandler(character5_MouseClick);
                this.character6.MouseClick += new MouseEventHandler(character6_MouseClick);
                this.character7.MouseClick += new MouseEventHandler(character7_MouseClick);
            }
            
        }

        private void character1_MouseClick(object sender, EventArgs e)
        {
            int idx = 0;
            Bitmap charsign = signs[idx];
            //this.character1;
            CharacterSave Character = new CharacterSave(charsign);
            Character.MdiParent = this.MdiParent;
            Character.Show();
        }

        private void character2_MouseClick(object sender, EventArgs e)
        {
            int idx = 1;
            CharacterSave Character = new CharacterSave(signs[idx]);
            Character.Show();
        }

        private void character3_MouseClick(object sender, EventArgs e)
        {
            int idx = 2;
            CharacterSave Character = new CharacterSave(signs[idx]);
            Character.Show();
        }

        private void character4_MouseClick(object sender, EventArgs e)
        {
            int idx = 3;
            CharacterSave Character = new CharacterSave(signs[idx]);
            Character.Show();
        }

        private void character5_MouseClick(object sender, EventArgs e)
        {
            int idx = 4;
            CharacterSave Character = new CharacterSave(signs[idx]);
            Character.Show();
        }

        private void character6_MouseClick(object sender, EventArgs e)
        {
            int idx = 5;
            CharacterSave Character = new CharacterSave(signs[idx]);
            Character.Show();
        }

        private void character7_MouseClick(object sender, EventArgs e)
        {
            int idx = 6;
            CharacterSave Character = new CharacterSave(signs[idx]);
            Character.Show();
        }
 //       private void character1_MouseClick(object sender, MouseEventArgs e)
 //       {
 ////           throw new NotImplementedException();
 //       }

        public List<Bitmap> signs ;
        private void buttonPlateRecognize_Click(object sender, EventArgs e)
        {
            try
            {
                GaborRec.GaborRecognize recog = new GaborRec.GaborRecognize();
                recog.Train();
                signs = this.Segment();
            string number="";
                //保存训练样本
            //for (int i = 0; i < signs.Count; i++)
            //    string path = Application.StartupPath+"\\"+i + ".bmp";
            //    signs[i].Save(path);
            //}
            for (int i = 0; i < signs.Count; i++)
            {
                char c = recog.Recognize(signs[i]);
                number += c;
            }
            this.textBoxPlateNumber.Text=number;
            }
            catch (System.Exception exp)
            {
                MessageBox.Show(this, exp.ToString());

            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void RecogForm_Load(object sender, EventArgs e)
        {
            if (this.Filename != null)
            {
                this.ori_Bitmap = new Bitmap(this.Filename);
                this.pictureBox1.Image = this.ori_Bitmap;
                this.Text = this.Filename;
            }
        }

        private void button_save_Click(object sender, EventArgs e)
        {
            try
            {
                string aftermodi;                
                aftermodi = this.textBoxPlateNumber.Text;
                CoCarImages coimg = new CoCarImages();
                coimg.SafaCarNumber(this.Filename,aftermodi);
            }
            catch(System.Exception exp)
            {
                MessageBox.Show(this, exp.ToString());
            }
            
        } 

    
    }
}
